<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Apply;
use app\admin\model\Course;
use app\admin\model\Coursetype;
use app\admin\model\Majorcourse;
use app\admin\model\Examtype;
use think\Request;

class  Courses extends Base
{
    //专业列表-包括专业分类-专业列表
    public function varlist(){
        $user = $this->user_info();
        $examtype = collection(Examtype::all())->toArray();
        $course = collection(Course::order("sort","asc")->select())->toArray();
        $applys = collection(Apply::where(['grade'=>date("Y")])->field("major_id,count(*) as c")->group("major_id")->select())->toArray();
        $arr = [];
        foreach($applys as $val) {
            $arr[$val['major_id']] = $val['c'];
        }
        //查询专业分类并转为id为key，name为value的数组
        $coursetype = collection(Coursetype::all())->toArray();
        //把专业放入专业类型中
        foreach($coursetype as $key=>$val){
            $coursetype[$key]['course'] = [];
            foreach ($course as $values) {
                $values['apply_number'] = array_key_exists($values['id'], $arr) ? $arr[$values['id']] : 0;
                if ($values['typeid'] == $val['id']) {
                    array_push($coursetype[$key]['course'], $values);
                }
            }
        }
        //把专业类型放入报考类型中
        foreach($examtype as $keys=>$vals){
            $examtype[$keys]['coursetype'] = [];
            foreach ($coursetype as $key => $val) {
                if ($val['examtype_id'] == $vals['id']){
                    array_push($examtype[$keys]['coursetype'],$val);
                }
            }
        }
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => $examtype,
        );
        return view("varlist",$data);
    }
    //添加专业分类
    public function addCourseType(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['id'] = null;
        //判断专业分类是否存在
        $count  = Coursetype::where(['examtype_id'=>$data['examtype_id'],'name'=>$data['name']])->find();
        if($count){
            return $this->error('该分类已存在');
        }
        //接收数据并且添加到专业分类表中
       $res =  Coursetype::insert($data);
       if($res){
          return  $this->success("添加分类成功","Courses/varlist",['tab'=>$data['examtype_id']]);
       }else{
          return  $this->error("添加分类失败");
       }
    }
    //添加专业
    public function addCourse(Request $request){
        $user = $this->user_info();
        //接收数据
        $data = $request->post();
        //判断该专业是否存在
        $count = Course::where(['examtype_id'=>$data['examtype_id'],'typeid'=>$data['typeid'],'name'=>$data['name']])->find();
        if($count){
          return  $this->error("该专业已存在");
        }
        //添加专业
        if (Course::insert($data)) {
           return  $this->success("添加专业成功", "Courses/varlist", ['tab' => $data['examtype_id'], 'typeid' => $data['typeid']]);
        } else {
           return  $this->error("添加专业失败");
        }


    }
    //修改专业
    public function updateCourse(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $count = Course::where(['examtype_id'=>$data['examtype_id'],'typeid'=>$data['typeid'],'name'=>$data['name'],'id'=>['not in',$data['id']]])->find();
        if($count){
           return  $this->error("修改的专业名称已存在");
        }
        if(Course::update($data)){
            return  $this->success("修改专业成功", "Courses/varlist", ['tab' => $data['examtype_id'], 'typeid' => $data['typeid']]);
        } else {
            return  $this->error("修改专业失败");
        }
    }
    //修改专业分类
    public function updateCourseType(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        //判断名称是否与其他的名称重复，如果不重复修改，如果重复则无法修改
        $const = Coursetype::where(['id'=>['not in',$data['id']],'examtype_id'=>$data['examtype_id'],'name'=>$data['name']])->find();
        if($const){
            return $this->error("修改的专业分类名称已经存在");
        }
        if(Coursetype::update($data)){
            return $this->success("修改专业分类成功","Courses/varlist",['tab'=>$data['examtype_id'],'typeid'=>$data['id']]);
        }else{
            return $this->error("修改专业分类失败");
        }
    }
    //删除专业分类
    public function delCourseType(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $count = Course::where(['typeid'=>$data['id']])->find();
        if($count){
            return $this->error("删除失败，该分类下还有专业");
        }
        if(Coursetype::destroy($data['id'])){
           return  $this->success("删除成功","Courses/varlist",['tab'=>$data['examtype_id']]);
        }else{
           return $this->error("删除失败");
        }
    }
    //删除专业
    public function delCourse(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        if(Course::destroy($data['id'])){
            return  $this->success("删除专业成功","Courses/varlist",['tab'=>$data['examtype_id'],'typeid'=>$data['typeid']]);
        }else{
            return $this->error("删除专业失败");
        }
    }

//------------------------------
    //添加
    public function addform(){
        $user = $this->user_info();
        $course = Coursetype::all();
        $data = array(
            "setting" => $this->getAllSetting(),
            "coursetype" => $course
        );
        return view("addform",$data);
    }
    //添加课程
    public function add(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $res = Course::where(["name"=>$data['name']])->find();
        if(!empty($res)){
          $this->success("课程已经存在编号为：{$res['id']}","Courses/varlist");
          return true;
        }
        if(Course::insert($data)){
            $this->success("添加课程成功","Courses/varlist");
        }else{
            $this->error("添加课程失败","Courses/addform");
        }
    }
    //编辑
    public function update(Request $request){
        $user = $this->user_info();
        $course = Coursetype::all();
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Course::where(array('id'=>$request->get('id')))->find(),
            "coursetype" => $course
        );
        return view("update",$data);
    }
    public function edit(Request $request){
        $data = $request->post();
        if(Course::update($data)){
            $this->success("更新课程成功","Courses/varlist");
        }else{
            $this->error("更新课程失败","Courses/update");
        }

    }
    //删除
    public function delete(Request $request){
        $user = $this->user_info();
        if(Course::destroy($request->post('id'))){
            $this->success("删除课程成功","Courses/varlist");
        }else{
            $this->error("删除课程失败","Courses/varlist");
        }
    }

  /*
   * 课程列表
   */
  public function course(Request $request){
    $user = $this->user_info();
    //查询所有的资讯分类，
    $resource = collection(Course::order('id','asc')->select())->toArray();
    //给查询每个分类下的所有资讯
    foreach($resource as $k => $v){
      $newslist = Majorcourse::where(array("type"=>$v['id']))->order("id desc")->paginate(30,false,[
        'path'=> "/public/admin/Courses/course/typeid/".$v['id'],
        'var_page' => 'page'.$v['id'],
      ]);
      $resource[$k]['newslist'] = $newslist;
    }
    $typeid = !empty($request->route("typeid")) ? $request->route("typeid") : $resource[0]['id'];
    $data = array(
      "setting" => $this->getAllSetting(),
      "activeid"=>$typeid,
      "list"=>$resource,
    );
    return view("course",$data);
  }

  //编辑
  public function majorcourse_update(Request $request){
    $user = $this->user_info();
    $data = array(
      "setting" => $this->getAllSetting(),
      "course" => Course::select(),
      "data" => Majorcourse::where(array('id'=>$request->get('id')))->find(),
    );
    return view("majorcourse_update",$data);
  }
  public function majorcourse_edit(Request $request){
    $data = $request->post();
    if(Majorcourse::update($data)){
      $this->success("更新课程成功","Courses/course?typeid=".$request->post('type'));
    }else{
      $this->error("更新课程失败","Courses/majorcourse_update");
    }
  }

  //删除
  public function deletes(Request $request){
    $user = $this->user_info();
    if(Majorcourse::destroy($request->post('id'))){
      $this->success("删除成功","Courses/course?typeid=".$request->post('typeid'));
    }else{
      $this->error("删除失败");
    }
  }

  //添加
  public function majorcourse_addform(){
    $user = $this->user_info();
    $data = array(
      "setting" => $this->getAllSetting(),
      "course" => Course::select(),
    );
    return view("majorcourse_addform",$data);
  }
  public function majorcourse_add(Request $request){
    $user = $this->user_info();
    if(Majorcourse::insert($request->post())){
      $this->success("添加成功","Courses/course?typeid=".$request->post('type'));
    }else{
      $this->error("添加失败","Courses/majorcourse_addform");
    }
  }


}
